package com.jetinno.analysis

import timber.log.Timber

/**
 * Created by zhangyuncai.
 * Date: 2022/1/20
 */
/**
 * 查询状态
 */
object Analysis01 {
    @JvmStatic
    fun analysis(cmd: String) {
        try {

            val byteList = mutableListOf<String>()
            for (index in 0 until cmd.length / 2) {
                val multi = index * 2
                val byte = cmd.substring(0 + multi, 2 + multi)
                byteList.add(byte)
            }

            Timber.e(start)
            //帧头
            val frameHeaderHex = byteList[0]
            Timber.e("帧头:%s", frameHeaderHex)
            Timber.e(line)
            //功能码
            val funCodeHex = byteList[1]
            Timber.e("功能码:%s", funCodeHex)
            Timber.e(line)
            //起始地址
            val startAddr = byteList[3]
            Timber.e("起始地址:00 %s", startAddr)
            Timber.e(line)
            //数据长度
            val dataLength = byteList[4] + byteList[5]
            val dataLengthInt = dataLength.toInt(16);
            Timber.e("数据长度:%s→%d", dataLength, dataLengthInt)
            Timber.e(line)
            //数据内容
            val stringBuilder = StringBuilder()
            for (index in 0 until dataLengthInt) {
                stringBuilder.append(byteList[5 + 1 + index])
            }
            val contentData = stringBuilder.toString()
            Timber.e("数据内容:%s", contentData)
            Timber.e(line)
            when (startAddr) {
                "B6" -> startAddrB6(stringBuilder.toString())
            }
            //校验码
            val checkCode = byteList[byteList.size - 2] + byteList[byteList.size - 1]
            Timber.e("校验码:%s", checkCode)
            Timber.e(end)

        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    ///查询产品制作当前料盒
    @Throws(java.lang.Exception::class)
    private fun startAddrB6(contentData: String) {
        when (contentData) {
            "FF" -> {
                Timber.e("非制作")
            }
            "FC" -> {
                Timber.e("产品开始")
            }
            "FD" -> {
                Timber.e("出料完成")
            }
            "FE" -> {
                Timber.e("等待取杯")
            }
            else -> {
                Timber.e("正在制作料盒ID : ${contentData}")
            }
        }
        Timber.e(line)
    }
}